Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Superoperator noise for density matrix method #295

Merged
merged 11 commits into from
Aug 2, 2019

Conversation

chriseclectic
Copy link
Member

@chriseclectic chriseclectic commented Jul 26, 2019

Summary

Adds support for general superoperator noise for the density matrix simulation method.

Depends on #253

TODO

Details and comments

  • Adds Superoperator class (subclass of DensityMatrix/QubitVector).
  • Adds function to QuantumError class to convert to Superoperator via superoperator simulation of noise sub circuits
  • Adds option to noise model to sample noise as full error superoperators rather than default stochastic noise sampling.
  • Adds superoperator noise sampling to QasmSimulator for the density matrix method
  • Moves QubitVector and its subclasses into separate folder.
  • Move state methods to seperate src/state-methods folder, and only have controllers in src/simulators

Example

An example of where this is useful is running noisy small qubit circuits with Kraus errors (eg something like randomized benchmarking simulations).

For the following example timing results on my laptop are:

  • Statevector method: 25 seconds
  • Density matrix method: 0.025 seconds
import numpy as np
from qiskit import *
from qiskit.compiler import assemble
from qiskit.providers.aer import QasmSimulator
from qiskit.providers.aer.noise import NoiseModel
from qiskit.providers.aer.noise.errors import thermal_relaxation_error

sim = QasmSimulator()

# Error model
noise_model = NoiseModel()
error = thermal_relaxation_error(50, 100, 50)
noise_model.add_all_qubit_quantum_error(error, ['u1', 'u2', 'u3'], False)

# Test circuit with noisy random u3 gates
nq = 2
depth = 1000
shots = 2000
qr = QuantumRegister(nq)
cr = ClassicalRegister(nq)
circ = QuantumCircuit(qr, cr)
for j in range(depth):
    for j in range(nq):
        a, b, c = np.random.rand(3)
        circ.u3(a, b, c, qr[j])
    circ.barrier(qr)
circ.measure(qr, cr)
qobj = assemble(circ, sim, shots=shots)

# Execute on statevector simulator
opts_sv = {"method": "statevector"}
results_sv = sim.run(qobj,
                 noise_model=noise_model,
                 backend_options=opts_sv).result()
time_sv = np.round(results_sv.results[0].time_taken, 3)
print("Execution time: {}".format(time_sv))
# ~25 seconds on Macbook

# Execute on density matrix simulator
opts_dm = {"method": "density_matrix"}
results_dm = sim.run(qobj,
                 noise_model=noise_model,
                 backend_options=opts_dm).result()
time_dm = np.round(results_sv.results[0].time_taken, 3)
print("Execution time: {}".format(time_dm))
# ~0.025 seconds on Macbook

@chriseclectic chriseclectic requested a review from atilag as a code owner July 26, 2019 14:24
@atilag atilag added this to the 0.3 milestone Jul 30, 2019
@chriseclectic chriseclectic force-pushed the feature/superoperator-noise branch 2 times, most recently from 63b7002 to 212506a Compare July 30, 2019 23:48
@chriseclectic
Copy link
Member Author

@atilag I rebased to remove the src directory restructuring from this PR so its easier to review, I will add that as a final PR after the others are merged

@chriseclectic chriseclectic force-pushed the feature/superoperator-noise branch from 212506a to b59e13b Compare August 1, 2019 04:30
atilag
atilag previously approved these changes Aug 1, 2019
Copy link
Member

@atilag atilag left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job again!

src/noise/noise_model.hpp Show resolved Hide resolved
src/noise/noise_model.hpp Outdated Show resolved Hide resolved
src/noise/noise_model.hpp Show resolved Hide resolved
src/noise/noise_model.hpp Show resolved Hide resolved
src/noise/noise_model.hpp Show resolved Hide resolved
//-----------------------------------------------------------------------

Superoperator() : Superoperator(0) {};
explicit Superoperator(size_t num_qubits);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What implicit conversion are we trying to avoid here? ... not sure if explicit makes sense.. but in any case, is not bothering.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can assing num_qubits_ in the initializer list of the ctor:

explicit Superoperator(size_t num_qubits) : num_qubits_(set_num_qubits(num_qubits)){
 }

this used to save some time at object construction time (at least the last time I saw it, It may not be the case now)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't compile for me

// Constructors & Destructor
//------------------------------------------------------------------------------

template <typename data_t>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see comment above (in the ctor)

}

// Return the set of qobj snapshot types supported by the State
virtual stringset_t allowed_snapshots() const override {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In our TODO list, we have to decople Qobj as much as possible.. just in case we change the format in the future (Protobuffer... is coming...)

const std::vector<Operations::Op> &ops) {
// An n-qubit unitary as 2^4n complex doubles
// where each complex double is 16 bytes
(void)ops; // avoid unused variable compiler warning
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can remove this ungly c-casting style by setting a default value for the ops parameter...

..., onst std::vector<Operations::Op> &ops = {}) {

Did you have the chance to check if this generates another unsed variable warning?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even if it has a default value, it will still be unused in the function body

// An n-qubit unitary as 2^4n complex doubles
// where each complex double is 16 bytes
(void)ops; // avoid unused variable compiler warning
size_t shift_mb = std::max<int_t>(0, num_qubits + 4 - 20);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why - 20 ?
Can we add a comment or better, use a const with a name?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shift by -20 converts bytes to MiB

@chriseclectic chriseclectic changed the title [WIP] Superoperator noise for density matrix method Superoperator noise for density matrix method Aug 1, 2019
Copy link
Member

@atilag atilag left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merging!

@atilag atilag merged commit bb2ac97 into Qiskit:master Aug 2, 2019
@chriseclectic chriseclectic deleted the feature/superoperator-noise branch October 4, 2019 15:31
dcmckayibm pushed a commit to dcmckayibm/qiskit-aer that referenced this pull request Nov 3, 2019
* Add superoperator simulator
* Add superoperator sampling method to noise model
* Add superoperator noise to density matrix qasm controller
* Remove AbstractError and add superop to QuantumError
* Fix measure sampling test for density matrix method
* Fix bug in superoperator matrix multiplication
   - Was using the wrong qubit number to calculate qubitvector multiplication indexes.
* Use QasmBackendConfiguration
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants